<template>
    <div class="shopCar-container">
        <!--商品列表区域-->
        <div class="goods-list" v-for="(item,i) in goodsList" :key="item.i" v-show="!showCar">
            <div class="mui-card">
                <div class="mui-card-content">
                    <div class="mui-card-content-inner">
                        <mt-switch v-model="$store.getters.getGoodsSelected[item.id]"
                        @change="selectedChanged(item.id,$store.getters.getGoodsSelected[item.id])"></mt-switch>
                        <div class="goodImg"><img :src="item.url" alt=""></div>
                        <div class="info">
                            <h4>{{item.title}}</h4>
                            <p>
                                <span class="price">￥{{item.price}}</span>
                                <numbox :initcount="$store.getters.getGoodsCount[item.id]"
                                :goodsid="item.id"></numbox>
                                <a href="#" @click.prevent = "remove(item.id,i)">删除</a>
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="mui-card" v-show="showCar">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <div class="empty">
                        <p>您的购物车空空是也！请到 首页 商品购买，选择你喜欢的商品！</p>
                    </div>
                </div>
            </div>
        </div>
        <!--结算区域-->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <div class="left">
                        <p>总计（不含运费）</p>
                        <p>已勾选商品 <span class="red">{{$store.getters.getGoodsCountAndAmount.count}}</span> 件，
                            总价 <span class="red">￥{{$store.getters.getGoodsCountAndAmount.amount}}</span>
                        </p>
                    </div>
                    <mt-button type="danger">去结算</mt-button>
                </div>
            </div>
        </div>
        <!--<p>{{$store.getters.getGoodsSelected}}</p>-->
    </div>

</template>
<script>
    import numbox from '../subcomponents/shopcar_numbox.vue';
    export default{
        data(){
            return {
//                goodsList: this.$store.state.car,
                goodsList: '',
                showCar:false
            }
        },
        created(){
            this.goodsList = this.$store.getters.getNewCar;
        },
        methods: {
            //删除购物车中的某件商品
            remove(id,index){
                this.goodsList.splice(index,1);
                this.$store.commit('removeFromCar',id);
            },
            //每当点击开关，把最新数据同步到store上
            selectedChanged(id,val){
                this.$store.commit('updateGoodsSelected',{id,selected:val})
            },
        },
        watch:{
            goodsList:function (newVal,oldVal) {
                if(newVal == null){
                    this.showCar = true;
                }else if(newVal.length<=0){
                    this.showCar = true;
                } else {
                    this.showCar = false;
                }
            }
        },
        computed: {
        },
        components: {numbox}
    }
</script>
<style lang="scss" scoped>
.shopCar-container{
    background: #EEEEEE;
    overflow: hidden;
    .goods-list{
        .mui-card-content-inner{
            display: flex;
            align-items: center;
            justify-content: space-around;
        }
        .goodImg{
            width: 60px;
            height: 80px;
            display: flex;
            align-items: center;
            img{
                width: 100%;
            }
        }

        h4{
            font-size: 16px;
        }
        .info{
            display: flex;
            flex-flow: column;
            p{
                display: flex;
                align-items: center;
                justify-content: space-around;
                .price{
                    color: red;
                    font-weight: bold;
                    margin-right: 5px;
                }
                a{
                    margin-left: 5px;
                }
            }
        }

    }
    .mui-card-content-inner{
        display: flex;
        justify-content: space-between;
        align-items: center;
        .left{
            p{
                .red{
                    color: red;
                    font-size: 16px;
                    font-weight: bold;

                }
            }
        }
    }
}
</style>